Ext.define('AM.view.user.UserList', {
    extend: 'AM.default.DefaultList',
    
    searchDepID: null,
    initComponent: function(){
    	this.columns = [{
            header: "账号",
            dataIndex: 'u.login_id',
            width: 80
        }, {
            header: user_lang.columns_userName,
            dataIndex: 'u.user_name',
            width: 80
        }, {
            header: '性别',
            dataIndex: 'u.sex_code',
            renderer: function(v){
                if (v == '1079') {
                    return '女';
                }
                else 
                    if (v == '1078') {
                        return '男';
                    }
            },
            width: 45
        }, {
            header: '年龄',
            dataIndex: 'age',
            sortable: false,
            width: 45
        }, {
            header: '民族',
            dataIndex: 'u.folk_code',
            width: 90,
            renderer: function(value, metaData, record){
                metaData.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: "职务",
            dataIndex: 'dutyRemark',
            sortable: false,
            minWidth: 110,
            flex: 1,
            renderer: function(value, metaData, record){
                metaData.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '联系电话',
            dataIndex: 'mobileTelephone',
            sortable: false,
            width: 90
        }, {
            header: '备注',
            dataIndex: 'remark',
            sortable: false,
            minWidth: 110,
            flex: 1
        }, {
            header: "最后操作时间",
            dataIndex: 'u.operate_time',
            width: 90,
            renderer: function(value, metaData, record){
                metaData.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: "是否可操作",
            dataIndex: 'isOperate',
            width: 80,
            renderer: function(value, metaData, record){
            	if(value ==1){
            		return "<span style='color:red;'>是</span>";
            	}else{
            		return "<span style='color:green;'>否</span>";
            	}
            }
        }/*, {
            menuDisabled: true,
            sortable: false,
            // 禁止调整列宽
            resizable: false,
            hideable: false,
            xtype: 'actioncolumn',
            header: user_lang.columns_operating,
            width: 60,
            items: [{
                tooltip: user_lang.columns_operation_tooltip,
                iconCls: "roleDistributionIcon",
                handler: function(grid, rowIndex, colIndex, item, eve, record){
                    var roleIdStr = record.get("roleIds");
                    var roleIds = Ext.create("Ext.util.MixedCollection");
                    if (roleIdStr != "") {
                        var temp = roleIdStr.split(",");
                        Ext.each(temp, function(value, index){
                            roleIds.add(value, value);
                        });
                    }
                    Ext.create("AM.view.userRole.UserRoleWindow", {
    					title: user_lang.userRoleTitle[0] + record.get("u.userName") + user_lang.userRoleTitle[1],
                        userID: record.get("userId"),
                        userName: record.get("u.userName"),
                        roleIds: roleIds,
                        grid: grid
                    });
                    
                }
            }]
        }*/];
        this.store = Ext.create('AM.'+ prototype +'.user.UserList');
        
        this.callParent(arguments);
    },
    
    onAddClick: function(button){
        var parentID = this.searchDepID;
        if (parentID == null) {
            Ext.Msg.show({
                title: orchid_lang.promptWindowInfo.warningTitle,
                msg: user_lang.addWarningMsg[0],
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
        }
        else 
            if (parentID == -1) {
                Ext.Msg.show({
                    title: orchid_lang.promptWindowInfo.warningTitle,
                    msg: user_lang.addWarningMsg[1],
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
            }
            else {
                var win = Ext.create('AM.view.user.UserEdit', {
                    grid: this
                });
                win.down("form").getForm().findField('departmentId').setValue(parentID);
            }
    },
    
    onUpdateClick: function(button){
    	var me = this;
        var record = this.getSelectionModel().getSelection()[0];
        var win = Ext.create('AM.view.user.UserEdit', {
            activeState: record.get("u.activeState"),
            grid: this
        });
        win.down('form').getForm().load({
			url: me.store.perUpdateUrl,
            params: {
                searchUserId: record.get("userId")
            },
            success: function(baseForm, action){
//                baseForm.findField("tpassword").setValue(action.result.data.password);
            }
        });
    },
    onReuseClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("u.activeState") == 1) {
                Ext.Msg.show({
                    title: orchid_lang.promptWindowInfo.warningTitle,
                    msg: orchid_lang.promptWindowInfo.reuseWarningMsg,
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
        Ext.getCmp("loadingMask").show();
        Ext.Ajax.request({
				url: me.store.tokenUrl,
            success: function(response){
                Ext.getCmp("loadingMask").hide();
                var data = response.responseText;
                var token = Ext.decode(data, true);
                Ext.Msg.confirm({
                    title: orchid_lang.promptWindowInfo.promptTitle,
                    msg: orchid_lang.promptWindowInfo.reusePromptText,
                    buttons: Ext.Msg.OKCANCEL,
                    fn: function(id){
                        if (id == "ok") {
                            Ext.getCmp("loadingMask").show();
                            var userIDs = new Array();
                            Ext.Array.each(records, function(name, index){
                                userIDs.push(name.get("userId"));
                            });
                            Ext.Ajax.request({
								url: me.store.reuseUrl,
                                params: {
                                    searchIsEnabled: 1,
                                    searchUserIds: userIDs,
                                    "springmvc.TOKEN": token
                                },
                                success: function(response){
                                    Ext.getCmp("loadingMask").hide();
                                    var data = response.responseText;
                                    var json = Ext.decode(data, true);
                                    var msg = json.msg;
                                    if (!msg && !json.success) {
                                        msg = orchid_lang.promptWindowInfo.reuseFailureText;
                                    }
//                                    Ext.Msg.alert('消息', json.msg, function(){
                                    Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, orchid_lang.promptWindowInfo.reuseSuccessText, function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                            });
                        }
                        else {
                            return;
                        }
                        
                    }
                });
            }
        });
    },
    
    onDeleteClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("u.activeState") == 2) {
                Ext.Msg.show({
                    title: orchid_lang.promptWindowInfo.warningTitle,
                    msg: user_lang.deleteInfo.deleteWarningMsg,
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
        Ext.getCmp("loadingMask").show();
        Ext.Ajax.request({
		url: me.store.tokenUrl,
            success: function(response){
                Ext.getCmp("loadingMask").hide();
                var data = response.responseText;
                var token = Ext.decode(data, true);
                Ext.Msg.confirm({
                    title: orchid_lang.promptWindowInfo.promptTitle,
                    msg: user_lang.deleteInfo.deletePromptMsg,
                    buttons: Ext.Msg.OKCANCEL,
                    fn: function(id){
                        if (id == "ok") {
                            Ext.getCmp("loadingMask").show();
                            var userIDs = new Array();
                            Ext.Array.each(records, function(name, index){
                                userIDs.push(name.get("userId"));
                            });
                            Ext.Ajax.request({
                                url: me.store.discardUrl,
                                params: {
                                    searchIsEnabled: 2,
                                    searchUserIds: userIDs,
                                    "springmvc.TOKEN": token
                                },
                                success: function(response){
                                    Ext.getCmp("loadingMask").hide();
                                    var data = response.responseText;
                                    var json = Ext.decode(data, true);
                                    var msg = json.msg;
                                    if (!msg && !json.success) {
                                        msg = user_lang.deleteInfo.deleteFailureMsg;
                                    }
//                                    Ext.Msg.alert('消息', json.msg, function(){
                                    Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, user_lang.deleteInfo.deleteSuccessMsg, function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                            });
                        }
                        else {
                            return;
                        }
                    }
                });
            }
        });
    },
  //同步
    onRefurbishClick: function(button){
    	var me = this;
        Ext.Msg.confirm({
            title: '提示',
            msg: "您确定要同步这些信息吗?",
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id){
                if (id == 'ok') {
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                    		loadType: gj_onlineLearning_constant.loadType,
                            url: me.store.refurbishUrl,
                    		extraParams: {
                    			searchTreepath: me.searchTreepath
                    		}
                    	});
                    	store1.load(function(rec, operation, success){
                    		var msg = success==true?operation.response.msg:operation.error;
                    		Ext.Msg.alert('消息', msg, function(){
                    			if(success){
                    				me.store.load();
                    			}
                    		});
                    	});
                    }else{
                        Ext.Ajax.request({
                            url: me.store.refurbishUrl,
                            params: {
                            	searchTreepath: me.searchTreepath
                            },
                            success: function(response){
                            	var data = response.responseText;
                            	var json = Ext.decode(data, true);
                            	Ext.Msg.alert('消息', json.msg, function(){
                            		if (json.success) {
                            			me.store.load();
                            		}
                            	});
                            }
                        });
                    }
                }
                else {
                    return;
                }
            }
        });
    },
    //配置角色
    onRoleDistributionClick: function(){
    	var me = this;
    	var records = this.getSelectionModel().getSelection();
        if(records.length > 1){
        	Orchid.alert("提示","一次只能操作一条数据！",function(){
        		return;
        	});
        }else{
        	var roleIdStr = records[0].get("roleIds");
            var roleIds = Ext.create("Ext.util.MixedCollection");
            if (roleIdStr != "") {
                var temp = roleIdStr.split(",");
                Ext.each(temp, function(value, index){
                    roleIds.add(value, value);
                });
            }
            Ext.create("AM.view.userRole.UserRoleWindow", {
    			title: user_lang.userRoleTitle[0] + records[0].get("u.userName") + user_lang.userRoleTitle[1],
                userID: records[0].get("userId"),
                userName: records[0].get("u.userName"),
                roleIds: roleIds,
                grid: me
            });
        }
    },
    listeners: {
    	//点击列表数据时显示按钮事件
        select: function(selModel, selected){
			var me = this;
	    	var records=me.getSelectionModel().getSelection();
       	 	//this.down('#' + this.oldId + "RoleDistribution").setDisabled(records.length === 0);//角色分配
       	 	if(records.length != 1){
       	 		this.down('#' + this.oldId + "Update_").setDisabled(true);
       	 	}else{
       	        for (var i = 0; i < records.length; i++) {
       	            if (records[i].get("isOperate") == 1) {
                    	this.down('#' + this.oldId + "Update_").setDisabled(false);
       	            }else{
       	            	this.down('#' + this.oldId + "Update_").setDisabled(true);
       	            }
       	        }
           }
		},
		deselect: function(selModel, selected){
			var me = this;
	    	var records=me.getSelectionModel().getSelection();
	    	//this.down('#' + this.oldId + "RoleDistribution").setDisabled(records.length === 0);//角色分配
	    	if(records.length != 1){
       	 		this.down('#' + this.oldId + "Update_").setDisabled(true);
       	 	}else{
       	        for (var i = 0; i < records.length; i++) {
       	            if (records[i].get("isOperate") == 1) {
                    	this.down('#' + this.oldId + "Update_").setDisabled(false);
       	            }else{
       	            	this.down('#' + this.oldId + "Update_").setDisabled(true);
       	            }
       	        }
           }
		},
        viewready: function(){
            this.addDocked([{
                xtype: 'toolbar',
                store: this.store,
                dock: 'top',
                displayInfo: true,
                items: [/*this.addButton, '-', */{
                    text: orchid_lang.buttons.updateButtonText,
                    iconCls: 'editIco',
                    itemId: this.oldId + "Update_",
                    disabled: true,
                    handler: function(button){
                        if (button.up("grid").getSelectionModel().getCount() > 1) {
                            Ext.Msg.show({
                                title: orchid_lang.promptWindowInfo.warningTitle,
                                msg: default_lang.defaultList.operatingWarningMsg,
                                icon: Ext.Msg.WARNING,
                                buttons: Ext.Msg.OK
                            });
                            return;
                        }
                        else {
                            button.up("grid").onUpdateClick(button);
                        }
                    }
                }, '-', this.reuseButton, '-', {
                    text: user_lang.tbarButtons[0],
                    iconCls: 'offIco',
                    itemId: this.oldId + "Delete",
                    disabled: true,
                    scope: this,
                    handler: this.onDeleteClick
                }/*, '-', {
                    text: "同步",
                    iconCls: 'refurbishIco',
                    scope: this,
                    handler: this.onRefurbishClick
                }, '-', {
                    text: "角色分配",
                    iconCls: 'roleDistributionIcon',
                    itemId: this.oldId + "RoleDistribution",
                    disabled: true,
                    scope: this,
                    handler: this.onRoleDistributionClick
                }*/, "->", "->", this.countSelectPanel]
            }]);
            Ext.getCmp("loadingMask").hide();
            // 触发权限过滤事件
            this.fireEvent("permission", this);
        },
        beforerender: function(){
            Ext.getCmp("loadingMask").show();
        }
    }

});
